//
//  DetailsWebViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/4/28.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "DetailsWebViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "YoungAllData.h"
#import "UMSocial.h"
#import "GMDCircleLoader.h"
#import "UMSocialSnsService.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"
@interface DetailsWebViewController ()<UIWebViewDelegate,YoungDelegate,UMSocialUIDelegate>
{
    UIActivityIndicatorView *activity;
    
}
@property(nonatomic,retain)NSString *zUrl;
@property(nonatomic,retain)NSString *zTitle;
@end

@implementation DetailsWebViewController
@synthesize courseIDStr,zUrl,zTitle;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNav];
    navTitle.text = @"培训内容";
    
    UIButton *backNewsButto1n = [UIButton buttonWithType:UIButtonTypeCustom];
    backNewsButto1n.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-50,SCREEN_WIDTH/2, 50);
    [backNewsButto1n addTarget:self action:@selector(detailViewBackAction1) forControlEvents:UIControlEventTouchUpInside];
    [backNewsButto1n setImage:[UIImage imageNamed:@"资料-推广素材---内容页_02"] forState:UIControlStateNormal];
    [self.view addSubview:backNewsButto1n];
    NSLog(@"%2f",SCREEN_WIDTH);
    UIButton *backNewsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backNewsButton.frame = CGRectMake(SCREEN_WIDTH/2, [UIScreen mainScreen].bounds.size.height-50,SCREEN_WIDTH/2, 50);
    [backNewsButton addTarget:self action:@selector(detailViewBackAction) forControlEvents:UIControlEventTouchUpInside];
    [backNewsButton setImage:[UIImage imageNamed:@"资料-推广素材---内容页_03"] forState:UIControlStateNormal];
    [self.view addSubview:backNewsButton];
    
    
    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate= self;
    [data gainTrainDetails:courseIDStr];
    
    
}
#pragma mark - 培训内容回调
-(void)trainDetails:(NSDictionary *)sign{
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
        zUrl = [NSString stringWithFormat:@"%@",[[sign objectForKey:@"data"] objectForKey:@"Content"]];
        zTitle = [NSString stringWithFormat:@"%@",[[sign objectForKey:@"data"] objectForKey:@"Subject"]];
        [self initUI:[[sign objectForKey:@"data"] objectForKey:@"Content"]];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma mark - 培训内容回调失败
-(void)requistFail{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
#pragma mark - 返回
-(void)detailViewBackAction{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -分享
-(void)detailViewBackAction1{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"507fcab25270157b37000010"
                                      shareText:zTitle
                                     shareImage:[UIImage imageNamed:@"ios-57.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatTimeline,nil]
                                       delegate:self];
    
    [UMSocialWechatHandler setWXAppId:@"wxe42555fc43cf3c1d" appSecret:@"a2348ea607d68e3995472e1cd4d56012" url:zUrl];
}
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response{
    NSLog(@"%@",response);
    NSDictionary *dic = (NSDictionary *)response;
    if ([[[dic objectForKey:@"data"] objectForKey:@"responseCode"]isEqualToString:@"200"]) {
        NSLog(@"success");
    }else{
        NSLog(@"Fail");
    }
}
#pragma mark - initUI
-(void)initUI:(NSString *)Content{
    UIWebView *webView = [[UIWebView alloc] init];
    webView.frame = CGRectMake(0, navView.bottom, self.view.frame.size.width, self.view.frame.size.height-64-50);
    [self.view addSubview:webView];
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",Content]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    webView.scalesPageToFit = YES;
    webView.delegate = self;
    
    
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];
    NSLog(@"zhengzai jiazai");
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"wancheg");
    [GMDCircleLoader hideFromView:self.view animated:YES];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"加载失败");
    [activity stopAnimating];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"加载失败，请稍后再试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

-(void)clickBackBtn{
    
    [self.navigationController popViewControllerAnimated: YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}
-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
